<?php

namespace app\admin\service\demo;

use app\admin\service\BaseService;
use app\common\constant\ErrorCode;
use app\common\model\demo\Demo;
use app\common\utils\MyException;
use app\common\utils\MyLog;

class DemoService extends BaseService
{
    public static function getDemoList($params)
    {
        //异常情况尽可能使用 throw new MyException，传入错误码
        //所有的错误码和错误文案统一管理：app/common/code/{xxx}Code.php
        if(empty($params['name'])){
            MyLog::record()->warning($params, "错误的name值,请留意!");
            throw new MyException(ErrorCode::CODE_PARAMS_ERROR);
        }

        //调试信息或者需要收集的数据通过MyLog统一记录
        //log内容可以传入任意数据类型：数组、json、字符串、数值
        MyLog::record()->notice(['name'=>$params['name']],"参数name值");

        //调用 model 层对数据库增删改查
        $list = Demo::getAllList();
        if(empty($list)){
            return [];
        }

        foreach($list as &$v){
            $v['type_name'] = Demo::TYPE_MAP[$v['type']] ?? '';
            $v['status_name'] = Demo::STATUS_MAP[$v['status']] ?? '';
        }
        return $list;
    }
}